﻿using SqlSugar;
using System.Data;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Modularity;
namespace Flies.SQLSugarCore.Tool
{
    public static class ModuleHelper
    {
        public static Type[] GetEntityTypesFromAllModules()
        {
            // 获取当前应用的所有模块
            var modules = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(a => a.GetTypes())
                .Where(t => t.IsClass && !t.IsAbstract && typeof(IAbpModule).IsAssignableFrom(t))
                .ToList();

            // 从模块中收集所有定义的实体类型
            var entityTypes = new List<Type>();

            foreach (var moduleType in modules)
            {
                // 获取模块的 Assembly
                var assembly = moduleType.Assembly;

                // 获取所有实现 IEntity 的非抽象类型
                var types = assembly.GetTypes()
                    .Where(t => t.IsClass && !t.IsAbstract && typeof(IEntity).IsAssignableFrom(t))
                    .ToList();

                entityTypes.AddRange(types);
            }

            return entityTypes.Distinct().ToArray();
        }
    }

}
